package com.test.demo.service.impl;

import com.test.demo.domain.UmsAdmin;
import com.test.demo.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

/**
 * 重写方法
 * 与用户信息关联
 * @author 远 chards_
 * @date: 2020-02-25 12:17
 */
@Component
public class UserDetailsImpl implements UserDetailsService {

    @Autowired
    private UmsAdminService umsAdminService;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //给每个用户授予USER权限
        List<GrantedAuthority> grantedAuthorities= new ArrayList<>();
        GrantedAuthority grantedAuthority=new SimpleGrantedAuthority("USER");
        grantedAuthorities.add(grantedAuthority);
        UmsAdmin umsAdmin = umsAdminService.get(s);

        //账号存在
        if (umsAdmin != null) {
            return new User(umsAdmin.getUsername(),umsAdmin.getPassword(),grantedAuthorities);
        }

        //账号不存在
        else {
            return  null;
        }
    }
}
